import Server from "@/config/server";
import request from '@/utils/request';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';

const state = {
  requestCode : false,
  dataLists: [],
  Fields: [],
  itemDetail: {},
  columnData: []
};
const actions = {
  //列表
  getLists({commit}, page) {
    request.get(Server.action.alarmRecord_list, { params: page })
    .then(response => {
      console.log(response);
        if (response.data.errorCode === 0) {
            commit("setLists", response.data.result.list);
            let fields = response.data.result.fields;
            fields.push({
                title: '操作',
                align: 'center',
                width: 60,
                scopedSlots: { customRender: 'action' },
                fixed: 'right',
            })
            fields[0].width = 60
            fields.forEach(function (item, i) {
              fields[i].dataIndex = fields[i].key
              fields[i].align = 'center';
              if(i < fields.length-2) {
                fields[i].width = item.width || 120
                if (item.key == 'alarm_time') {
                    item.width = 150                  
                }
                if (item.key == 'handle_status_name') {
                  item.scopedSlots = { customRender: 'handle_status_name' }
                }
              }
            })
            commit("setFields", fields);
        } else {

        }
    })
  },
  //编辑
  dataEdit({commit}, data) {
    commit("setErrorCode", false);
    request.get(Server.action.alarmRecord_handleStatus, { params: data } )
    .then(response => {
      console.log(response);
      commit("setErrorCode", response.data.errorCode);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
  // ID删除 > 参数为：[1,2,3,4,5,6]
  dataDel({ commit }, ids) {
    request.get(Server.action.alarmRecord_delete, { params: { id: ids } })
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          showSuccess('删除成功');
        } else {
          showError(response.data.message);
        }
      });
  },
  // 详情
  dataDetail({commit}, id) {
    request.get(Server.action.alarmRecord_detail + id)
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        commit("setDetail", response.data.result);
        // showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
};
const mutations = {
  setErrorCode(state, text) {
	    state.requestCode = text;
	},
	setLists(state, lists) {
	    state.dataLists = lists;
	},
  setFields(state, lists) {
	    state.Fields = lists;
	},
  setDetail(state, data) {
      state.itemDetail = data;
  },
  setColumnAll(state, data) {
    state.columnData = data;
  },

};
export default {
    namespaced: true,
    state,
    actions,
    mutations
};
